<?php
//二维码管理

namespace Admin\Controller;
use Think\Controller;
class QrcodeController extends CommonController {
    
    //生成二维码
    public function creatQr()
    {
        // $goods_id = I('get.goods_id');
        // $supplier_id = I('get.supplier_id');
        $this->assign('data',I('get.'));
        $this->display('creatqr');
    }
    //生成二维码  处理提交数据
    public function creatQrSub()
    {

        $goods_id = I('post.goods_id',0);
        $supplier_id = I('post.supplier_id',0);
        // 发放数量
        $send_sum = I('post.send_sum',1);
        //扫描次数
        $number = I('post.number',0);
        // print_r(I('post.')); 
        $j=0;
        while ($j < $send_sum)
        {
            $returnStr = $this->qr_sn();            
            $qr_sn = M('security_qr')->where('qr_sn='.$returnStr)->getField('qr_sn');
            if (!$qr_sn)
            {
               // $db->query("INSERT INTO ".$ecs->table('security_qr')." (supplier_id, goods_id,qr_sn, number,add_time) VALUES('$supplier_id', '$goods_id', '$returnStr', '$number', '$add_time')");
                $data = array(
                        'supplier_id' => $supplier_id,
                        'goods_id' => $goods_id,
                        'qr_sn' => $returnStr,
                        'number' => $number,
                        'add_time' => time(),
                    );
                M('security_qr')->add($data);
                $j++;

            }
        }
        $this->success('新增成功', U('Securityqr/fweimalist'));
    }
    //随机创建16位数
    function qr_sn(){
        $returnStr = substr(strrev(microtime()), 0, 3) .substr(strtotime(date("Y-m-d", time())), 0, 3) . substr(strrev(microtime()), 0, 3) . substr(mt_rand(), 0, 4) . substr(rand(), 0, 3);
        return $returnStr;
    }
    //基本信息
    public function goodsInfo()
    {
        $goods_id = I('get.goods_id');//分类ID
        $term_id = M('goods')->where('goods_id='.$goods_id)->getField('term_id');
        $otherinfo = M('field')->where('term_id='.$term_id)->select();
        $data = M('security_basics')->where(I('get.'))->find();
        $this->assign('data',I('get.'));
    //    if($data[''])
        foreach ($otherinfo as $key => $value) {
            if($value['form_type']=='radio')
            {
                //echo '1111';
                $otherinfo[$key]['setting'] = explode(',',$value['setting']);
            }
        }
       //var_dump($otherinfo);
        $sql = "show tables like 'fwzs_security_info_".$term_id."'";
		$Model = M();
		$result = $Model->query($sql);
      	 
      	if(!empty($result))
      	{
      		$buchonginfo = M('security_info_'.$term_id)->where('goods_id='.$goods_id)->find();
      	}else{
      		$buchonginfo = '';
      	}
        
        $this->assign('info',$data);
        $this->assign('buchonginfo',$buchonginfo);
        $this->assign('otherinfo',$otherinfo);
        $this->display('goodsinfo');
    }
     
    //追溯信息列表
    public function zhuisuInfo()
    {
        $goods_id = I('get.goods_id');
        $supplier_id = I('get.supplier_id');      
        $Obj = M('security_info'); // 实例化User对象
        //$map = array('status'=>'1');
        $where = "goods_id=$goods_id and supplier_id=$supplier_id";
        if(I('post.keywords',''))
        {
            $where .= " and supplier_name like '%".I('post.keywords')."%'";
        }
        $count      = $Obj->where($where)->count();// 查询满足要求的总记录数
        $Page = getpage($count, 8);
        
        $list = $Obj
        ->alias('a')
        ->field("a.*,t.name")
        ->join("left join fwzs_terms as t on t.term_id = a.cat_id")
        ->order('sort,id desc')
        ->where($where)
        ->limit($Page->firstRow, $Page->listRows)
        ->select();
        $this->assign('listinfo',$list);// 赋值数据集
        $this->assign('page',$Page->show());// 赋值分页输出 
        $this->assign('goods_id',$goods_id);
        $this->assign('supplier_id',$supplier_id);
        $this->display('zhuisuinfo');
    }
    //追溯信息添加
    public function addZhuisuInfo()
    {
         $goods_id = I('get.goods_id');
        $supplier_id = I('get.supplier_id');
       // print_r(I('get.'));
       //分类展示
        $field = array('term_id','name','parent_id');  
        $row = $this->allCategory($field);  
        //生成无限极分类  
        $list=$this->tree($row);  
        $this->assign('row',$list); 
        $this->assign('goods_id',$goods_id);
        $this->assign('supplier_id',$supplier_id);
        $this->display('addzhuisu');
    }
    //追溯信息添加  处理提交数据
    public  function addZhuisuSub()
    {
      //  echo $_POST['add_time'];
        $addtime = strtotime($_POST['add_time']);
        $data =array(
        'supplier_id'=>$_POST['supplier_id'],
        'goods_id'=>$_POST['goods_id'],
        'dcontent'=>$_POST['dcontent'],
        'fuzeren'=>$_POST['fuzeren'],
        'add_time'=>$addtime,
        'is_open'=>$_POST['is_open'],
        'cat_id'=>$_POST['cat_id'],
        'sort'=>$_POST['sort'],
        'title_name'=>$_POST['title_name'],
        'zy_con'=>$_POST['zy_con'],
        'shangjia'=>$_POST['shangjia'],
        );
        $re = M('security_info')->add($data);
      //  echo M('security_info')->getLastSql();
        if($re){    
          $this->success('添加成功');
        } else {   
         
         $this->error('请填写有效的信息！');
        }
  //  $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('security_info'), $data, 'INSERT');
    }
    //修改防伪追溯信息
    public function editZhuisuInfo()
    {
        $goods_id = I('get.goods_id');
        $supplier_id = I('get.supplier_id');
        //  信息内容
        $data = M('security_info')->where("id = ".I('get.id'))->find();
       // print_r(I('get.'));
       //分类展示
        $field = array('term_id','name','parent_id');  
        $row = $this->allCategory($field);  
        //生成无限极分类  
        $list=$this->tree($row);  
        $this->assign('row',$list); 
        $this->assign('goods_id',$goods_id);
        $this->assign('data',$data);

        $this->assign('supplier_id',$supplier_id);
        $this->display('editzhuisu');
    }
    //修改防伪追溯信息 数据处理
    public function editZhuisuInfoSub()
    {
        $addtime = strtotime($_POST['add_time']);
        $data =array(
        'supplier_id'=>$_POST['supplier_id'],
        'goods_id'=>$_POST['goods_id'],
        'dcontent'=>$_POST['dcontent'],
        'fuzeren'=>$_POST['fuzeren'],
        'add_time'=>$addtime,
        'is_open'=>$_POST['is_open'],
        'cat_id'=>$_POST['cat_id'],
        'sort'=>$_POST['sort'],
        'title_name'=>$_POST['title_name'],
        'zy_con'=>$_POST['zy_con'],
        'shangjia'=>$_POST['shangjia'],
        );
        $re = M('security_info')->where("id = ".I('post.id'))->save($data);
     //echo M('security_info')->getLastSql();
        if($re!==false){    
          $this->success('修改成功',U('Qrcode/qr_listc'));
        } else {   
         
         $this->error('请填写有效的信息！');
        }
    }
    //删除防伪追溯洗洗
    public function delZhuisuInfo()
    {
         $re = M('security_info')->where("id = ".I('get.id'))->delete();
         if($re!==false){    
          $this->success('删除成功');
        } else {   
         
          $this->error('删除失败，请重新删除');
        }
    }
    //防伪码  列表
    public function fwmList()
    {
        $goods_id = I('get.goods_id');
        $supplier_id = I('get.supplier_id');      
        $Obj = M('security_qr'); // 实例化User对象
        //$map = array('status'=>'1');
        $where = "a.goods_id=$goods_id and a.supplier_id=$supplier_id";
        if(I('post.keywords',''))
        {
            $where .= " and supplier_name like '%".I('post.keywords')."%'";
        }
        $count      = $Obj
        ->alias('a')
        ->field("a.*,b.goods_name")
        ->join("left join fwzs_goods as b on b.goods_id = a.goods_id")
        // ->join("left join fwzs_goods as b on b.goods_id = a.goods_id")
        ->order('a.qr_id desc')
        ->where($where)
        ->count();// 查询满足要求的总记录数
       
        $Page = getpage($count, 15);
        $list = $Obj
        ->alias('a')
        ->field("a.*,b.goods_name,c.supplier_name")
        ->join("left join fwzs_goods as b on b.goods_id = a.goods_id")
        ->join("left join fwzs_security as c on c.supplier_id = a.supplier_id")
        ->order('a.qr_id desc')
        ->where($where)
        ->limit($Page->firstRow, $Page->listRows)
        ->select();
        $this->assign('listinfo',$list);// 赋值数据集
        $this->assign('page',$Page->show());// 赋值分页输出 
        $this->assign('goods_id',$goods_id);
        $this->assign('supplier_id',$supplier_id);
         
        $this->display('fwmlist');
    }
    //冻结防伪码
    public function fwmDongjie()
    {
        $qr_id = I('get.qr_id');
        $re = M('security_qr')->where("qr_id=".$qr_id)->save(array('qr_start'=>1));
        if($re!==false){    
          $this->success('冻结成功');
        } else {   
         
          $this->error('冻结失败');
        }
    }
     //解冻防伪码
    public function fwmJiedong()
    {
        $qr_id = I('get.qr_id');
        $re = M('security_qr')->where("qr_id=".$qr_id)->save(array('qr_start'=>0));
        if($re!==false){    
          $this->success('解冻成功');
        } else {   
         
          $this->error('解冻失败');
        }
    }
    //删除防伪码
    public function delFwm()
    {
         $qr_id = I('get.qr_id');
        $re = M('security_qr')->where("qr_id=".$qr_id)->delete();
        if($re){    
          $this->success('删除成功');
        } else {   
         
          $this->error('删除失败');
        }
    }
     
  
     //检测信息
     public function jianceList()
     {
      //  echo '234234';
        $goods_id = I('get.goods_id');
        $supplier_id = I('get.supplier_id'); 
        $Obj = M('security_fwinfo'); // 实例化User对象
        //$map = array('status'=>'1');
        $where = "a.goods_id=$goods_id and a.supplier_id=$supplier_id";
        if(I('post.keywords',''))
        {
            $where .= " and supplier_name like '%".I('post.keywords')."%'";
        }
        $count      = $Obj
        ->alias('a')        
        ->order('a.id desc')
        ->where($where)
        ->count();// 查询满足要求的总记录数
        $Page = getpage($count, 15);
        
        $list = $Obj
        ->alias('a')
         
        ->order('a.id desc')
        ->where($where)
        ->limit($Page->firstRow.','.$Page->listRows)
        ->select();
        $this->assign('listinfo',$list);// 赋值数据集
        $this->assign('page',$Page->show());// 赋值分页输出 

        $this->assign('goods_id',$goods_id);
        $this->assign('supplier_id',$supplier_id);
        $this->display('jiancelist');
     }
     //添加检测信息
     public function addJiance()
     {
        $goods_id = I('get.goods_id');
        $supplier_id = I('get.supplier_id'); 
        $this->assign('goods_id',$goods_id);
        $this->assign('supplier_id',$supplier_id);
        $this->display('addjiance');
     }
     public function addJianceSub()
     {
         $jctime = strtotime($_POST['jctime']);
        $data =array(
        'supplier_id'=>$_POST['supplier_id'],
        'goods_id'=>$_POST['goods_id'],
        'jcxiangmu'=>$_POST['jcxiangmu'],
        'jcjieguo'=>$_POST['jcjieguo'],
        'jcdanwei'=>$_POST['jcdanwei'],
        'jcjieguo'=>$_POST['jcjieguo'],
        'fuzeren'=>$_POST['fuzeren'],
        'jctime'=>$jctime,
        'add_time'=>time(),
        'is_open'=>$_POST['is_open'],
        
        'sort'=>$_POST['sort'],
        );
   // $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('security_fwinfo'), $data, 'INSERT');
         $re = M('security_fwinfo')->add($data);
        if($re){    
          $this->success('添加成功',U('Qrcode/jianceList',array('goods_id'=>$_POST['goods_id'],'supplier_id'=>$_POST['supplier_id'])));
        } else {   
         
          $this->error('添加失败');
        }
     }
     public function delJiance()
     {
        $id = I('get.id');
        $re = M('security_fwinfo')->where('id='.$id)->delete();
        if($re){    
          $this->success('删除成功');
        } else {   
         
          $this->error('删除失败');
        }
     }
     //修改
     public function editJiance()
     {
        $id = I('get.id');
        $data = M('security_fwinfo')->where('id='.$id)->find();
        $this->assign('data',$data);
        $this->display('editjiance');
     }
     //修改 检测信息
     public function editJianceSub()
     {
         $jctime = strtotime($_POST['jctime']);
          $data =array(     
        'jcxiangmu'=>$_POST['jcxiangmu'],
        'jcjieguo'=>$_POST['jcjieguo'],
        'jcdanwei'=>$_POST['jcdanwei'],
        'jcjieguo'=>$_POST['jcjieguo'],
        'fuzeren'=>$_POST['fuzeren'],
        'jctime'=>$jctime,
        'is_open'=>$_POST['is_open'],       
        'sort'=>$_POST['sort'],
        );
   // $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('security_fwinfo'), $data, 'INSERT');
         $re = M('security_fwinfo')->where('id='.I('post.id'))->save($data);
        if($re){    
          $this->success('修改成功',U('Qrcode/jianceList',array('goods_id'=>$_POST['goods_id'],'supplier_id'=>$_POST['supplier_id'])));
        } else {   
         
          $this->error('修改失败');
        }
     }

}